package com.yc.api2;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/11/1
 * Email haijunzhou@hnit.edu.cn
 */
public class DateDemo {
    public static void main(String[] args) {
        Date now = new Date();
        Date date = new Date(0);

        System.out.println("now = " + now);
        System.out.println("date = " + date);
        System.out.println("now.after(date) = " + now.after(date));
        System.out.println("now.before(date) = " + now.before(date));
        // 时间戳 1970... 到现在的毫秒值
        System.out.println("now.getTime() = " + now.getTime());
        System.out.println("System.currentTimeMillis() = " + System.currentTimeMillis());

        System.out.println("date.getHours() = " + date.getHours());


        System.out.println("===============================");
        // 当前时间日历
        Calendar calendar = Calendar.getInstance();
        System.out.println("calendar = " + calendar);

        System.out.println(calendar.get(Calendar.MONTH));
        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));

        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("fmt.format(now) = " + fmt.format(now));
        System.out.println("fmt.format(date) = " + fmt.format(date));


        calendar.add(Calendar.MONTH,1);
        // calendar.getTime() 获取日期对象的日期对象
        System.out.println("月份+1 calendar = " + fmt.format(calendar.getTime()));
        calendar.add(Calendar.DAY_OF_MONTH,50);
        System.out.println("日期+50 calendar = " + fmt.format(calendar.getTime()));

        System.out.println("---------------计算2025元旦到现在还有多少天-------------------");

        Calendar yuandan = Calendar.getInstance();
        yuandan.set(Calendar.YEAR,2025);
        yuandan.set(Calendar.MONTH,0);
        yuandan.set(Calendar.DAY_OF_MONTH, 1);
        yuandan.set(Calendar.HOUR_OF_DAY, 0);
        yuandan.set(Calendar.MINUTE, 0);
        yuandan.set(Calendar.SECOND,0);
        System.out.println("元旦= " + fmt.format(yuandan.getTime()));

        Date ydate = yuandan.getTime();
        // 元旦时间戳 - 当前时间戳 = 两个日期的差值
        long time = ydate.getTime() - now.getTime();
        // 转成天数
        long days = time / 1000 / 60 / 60 / 24;
        System.out.println("2025元旦到现在还有" + days + "天");

    }




}
